<template>
    <div class="page-100 login-register-part">
        <yd-cell-item style="padding-bottom: 10px">
            <yd-input  slot="right" type="password" v-model="password" placeholder="登录密码"></yd-input>
        </yd-cell-item>
        <yd-cell-item style="padding-bottom: 10px">
            <yd-input slot="right"  v-model="mobile" regex="mobile" placeholder="新手机号"></yd-input>
        </yd-cell-item>
        <yd-cell-item style="padding-bottom: 10px">
            <input slot="right" type="text" placeholder="短信验证码" v-model="captcha" oninput="if (value.length>6) value=value.slice(0,6)" >
            <yd-sendcode slot="right"
                         v-model="startcode"
                         @click.native="sendCaptcha"
                         type="primary"
            ></yd-sendcode>
        </yd-cell-item>
        <yd-button size="large" type="primary" @click.native="revisePhoneClick" style="background-color:#3cc8a8;color:white;height: 40px">确定</yd-button>

    </div>
</template>

<script>
    import {mobileCheckPwd,captcha,checkMobile} from "../../pcof/post";
    import {userInfoData} from '../../globalVariables'

    export default {
        name: "revisePhone",
        data() {
            return {
                password:'',
                mobile: '',
                captcha: '',
                startcode: false,


            };
        },
        methods:{
            async sendCaptcha() {
                if (this.password === null || this.password === '') {
                    this.$dialog.toast({
                        mes: '请输入请输入登录密码!',
                        timeout: 1500
                    });
                    return;
                }

                var exp=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/;

                if(!exp.test(this.password))
                {
                    this.$dialog.toast({
                        mes: '密码格式错误',
                        timeout: 1500
                    });
                    return;
                }
                if (this.mobile === null || this.mobile === '') {
                    this.$dialog.toast({
                        mes: '请输入手机号!',
                        timeout: 1500
                    });
                    return;
                }
                if (this.mobile.length < 11) {
                    this.$dialog.toast({
                        mes: '手机输入有误！',
                        timeout: 1500
                    });
                    return;
                }
                if (this.startcode == false) {
                    this.$dialog.toast({
                        mes: '请发送验证码！',
                        timeout: 1500
                    });
                    return;
                }

                if (this.captcha === null || this.captcha === '') {
                    this.$dialog.toast({
                        mes: '请输入验证码!',
                        timeout: 1500
                    });
                    return;
                }
                //校验手机号application/checkMobile
                var mobileResponse = await checkMobile(this.mobile);

                if(mobileResponse.data.code === 200 )//正常的手机号
                {
                    this.$dialog.toast({
                        mes:  '该手机号已被使用！',
                        timeout: 3000
                    });
                    return;

                }else if (mobileResponse.data.code === 201 || mobileResponse.data.code === 202) {
                    //checkMobile 0不检查 1检查 注册
                    this.$dialog.loading.open('发送中...');
                    // customerType,mobile,checkMobile,

                    var response = await captcha(this.mobile);

                    if (response.data.code === 200) {
                        setTimeout(() => {

                            this.startcode = true;
                            this.$dialog.loading.close();
                            this.$dialog.toast({
                                mes: '已发送',
                                timeout: 1500
                            });

                        }, 1000);


                    } else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: response.data.msg,
                            timeout: 1500
                        });
                    }


                }else {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: mobileResponse.data.msg,
                        timeout: 1500
                    });
                }



            },
            async revisePhoneClick ()
            {
                this.$dialog.loading.open('修改中...');

                var uuid=JSON.parse(localStorage.getItem(userInfoData)).uuid;
                var  response = await  mobileCheckPwd(uuid,this.password,this.mobile,this.captcha);
                if (response.data.code === 200) {
                    this.$dialog.loading.close();

                    var dic=new Array();
                    dic =JSON.parse(localStorage.getItem(userInfoData));
                    var  newdic={};
                    for (var key in dic) {
                        if (key ==='mobile')
                        {
                            newdic[key]=this.mobile;
                        }else
                        {
                            newdic[key] = dic[key];
                        }
                    }
                    localStorage.removeItem(userInfoData);
                    localStorage.setItem(userInfoData,JSON.stringify
                    (newdic));
                    this.$router.back(-1);

                    this.$dialog.toast({
                        mes: '修改成功！',
                        timeout: 1500
                    });

                }else
                {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes:response.data.msg,
                        timeout: 1500
                    });
                }

            }

        },
        // async checkMobileBlur() {
        //
        //     //code 200-正常，201-未注册 202-未激活 // 校验手机号
        //     //失去焦点事件
        //     // 请求检查手机号接口获取authtoken
        //     var response = await checkMobile(this.mobile);
        //     localStorage.setItem(authToken,response.data.authtoken);
        //     this.phoneCode=response.data.code;
        //     console.log(this.phoneCode);
        //
        //     if (this.phoneCode === 200) {
        //
        //     }else if(this.phoneCode === 201)
        //     {
        //         // localStorage.setItem('mobile',this.mobile);
        //         //注册或激活账号 userState 0 是需要注册 1是需要激活 2是重置密码
        //
        //         this.userState='0';
        //         //1:未注册
        //         this.$dialog.confirm({
        //             title: '',
        //             mes: '该手机号尚未注册。',
        //             opts: [
        //                 {
        //                     txt: '确定',
        //                     color: true,
        //                     callback: () => {
        //                         //注册或激活账号 userState 0 是需要注册 1是需要激活 2是重置密码
        //                         this.$router.push({name: 'loginResetPwd', params: {userState: this.userState}});
        //                     }
        //                 }
        //             ]
        //         });
        //     }else if(this.phoneCode === 202)
        //     {
        //         this.userState='1';
        //         // localStorage.setItem('mobile',this.mobile);
        //         this.$dialog.confirm({
        //             title: '',
        //             mes: '该账号尚未激活，请先激活账号。',
        //             opts: [
        //                 {
        //                     txt: '确定',
        //                     color: true,
        //                     callback: () => {
        //                         //注册或激活账号 userState 0 是需要注册 1是需要激活 2是重置密码
        //                         this.$router.push({name: 'loginResetPwd', params: {userState: this.userState,moblie:this.moblie}});
        //                     }
        //                 }
        //             ]
        //         });
        //     }else if(this.phoneCode === 500)
        //     {
        //         this.$dialog.toast({
        //             mes: response.data.msg,
        //             timeout: 1500
        //         });
        //     }
        //
        // },


    }
</script>
   <style lang='scss'>
    @import "../login/login.scss";

</style>